﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Framework.Core
{
    public class UserBll:BaseBizLogic
    {
        /// <summary>
        /// 加载用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public User LoadUserBaseInfo(string userName)
        {
            try
            {
                ///加载用户基本信息
                User user = new EmployeeDataAccess().LoadUserInfo(userName);
                ///获取当前用户在当前登录酒店的角色
                List<UserRole> userRoles = new UserRoleDataAccess().GetUserRoles(user.Name, user.LoginHotelId);
                RoleInfoDataAccess roleDataAccess = new RoleInfoDataAccess();
                foreach (UserRole userRole in userRoles)
                {
                    if (user.Roles.Where(m => m.RoleID == userRole.RoleID).Count() == 0)
                        user.Roles.Add(roleDataAccess.GetRole(userRole.RoleID));
                }

                List<RulePage> rulePages = new RulePageDataAccess().GetRulePages();
                foreach (RoleInfo role in user.Roles)
                {
                    role.RuleInfos = new RuleInfoDataAccess().GetRoleRules(role.RoleID);
                    foreach (RuleInfo rule in role.RuleInfos)
                    {
                        rule.RulePages.AddRange(rulePages.Where(m => m.RuleID == rule.RuleID));
                    }
                }
                user.UserAuths = new UserAuthDataAccess().GetUserAuths(user.Name);
                return user;
            }
            catch (Exception ex)
            {                
                throw new FrameworkException(ExceptionCode.FRAMEWORK_DEFAULT_EXCEPTIONCODE, "加载用户信息失败", ex.InnerException);
            }
        }

        /// <summary>
        /// 验证用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public bool VerifyPassword(string userName,string passWord)
        {
            User user = new EmployeeDataAccess().VerifyPassword(userName, passWord);
            if (user == null)
            {
                ///用户名或密码错误
                return false;
            }
            else if (user.PasswordExpireDate < DateTime.Today)
            {
                ///用户已过期
                return false;
            }
            return true;
        }
    }
}
